﻿package com.thorqq.magictimer;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.baidu.mobstat.StatService;
import com.thorqq.magictimer.core.TLoopPolicy;
import com.thorqq.magictimer.timeLoop.QuickSetting;
import com.thorqq.magictimer.timeLoop.TimerMgr;
import com.thorqq.magictimer.util.Util;
import com.thorqq.magictimer.widget.LineBreakLayout;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class FragmentActivityAddLoopPolicy extends FragmentActivityBase
{
    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private List<String>   mTitleList    = new ArrayList<String>();    

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        initLayout();
        updateLayout();
        regListener();
    }
    
    private void regListener()
    {
    }

    private void initLayout()
    {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);        // 2013年
        int month = c.get(Calendar.MONTH) + 1;  // 从0开始。例如：0表示1月
        int day = c.get(Calendar.DAY_OF_MONTH); // 从1开始。1就表示1号
        int week = c.get(Calendar.DAY_OF_WEEK_IN_MONTH); // 从1开始，1表示第一个周几
        int weekday = c.get(Calendar.DAY_OF_WEEK);  // 1-7，从星期日开始
        
        //常用
        List<PolicyInfo> CommonPolicyList = new ArrayList<PolicyInfo>();
        try
        {
            CommonPolicyList.add(new PolicyInfo("每天", 
                    QuickSetting.class.getMethod("everyday", (Class[])null)));
            CommonPolicyList.add(new PolicyInfo("工作日", 
                    QuickSetting.class.getMethod("weekCycle", 
                            new Class[]{char[].class}),
                            new Object[]{new char[]{'0','1','1','1','1','1','0'}}));
            CommonPolicyList.add(new PolicyInfo("某月几号", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{month, day, 0}));
            CommonPolicyList.add(new PolicyInfo("某月的第几个周几", 
                    QuickSetting.class.getMethod("monthAndWeekDayCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{month,week,weekday}));
            CommonPolicyList.add(new PolicyInfo("从指定日开始持续n天", 
                    QuickSetting.class.getMethod("lastNDaysFromSpecifiedDate", 
                            new Class[]{int.class, int.class, int.class, int.class}),
                            new Object[]{year, month, day, 1}));
            CommonPolicyList.add(new PolicyInfo("指定日", 
                    QuickSetting.class.getMethod("specifiedDate", 
                            new Class[]{int.class, int.class, int.class, int.class}),
                            new Object[]{year, month, day, 0}));
            CommonPolicyList.add(new PolicyInfo("今天", 
                    QuickSetting.class.getMethod("specifiedDate", 
                            new Class[]{int.class, int.class, int.class, int.class}),
                            new Object[]{year, month, day, 0}));
            CommonPolicyList.add(new PolicyInfo("明天", 
                    QuickSetting.class.getMethod("specifiedDate", 
                            new Class[]{int.class, int.class, int.class, int.class}),
                            new Object[]{year, month, day+1, 0}));
            CommonPolicyList.add(new PolicyInfo("后天", 
                    QuickSetting.class.getMethod("specifiedDate", 
                            new Class[]{int.class, int.class, int.class, int.class}),
                            new Object[]{year, month, day+2, 0}));
        }
        catch (Exception e)
        {
            Util.log_ex(e);
        } 
        mFragmentList.add(new FragmentLoopPolicy(this, CommonPolicyList));
        mTitleList.add("常用");

            
        //高级
        List<PolicyInfo> SeniorPolicyList = new ArrayList<PolicyInfo>();
        try
        {
            SeniorPolicyList.add(new PolicyInfo("按天循环", 
                    QuickSetting.class.getMethod("everyday", (Class[])null)));
            SeniorPolicyList.add(new PolicyInfo("按周循环", 
                    QuickSetting.class.getMethod("weekCycle", 
                            new Class[]{char[].class}),
                            new Object[]{new char[]{'1','1','1','1','1','1','1'}}));
            SeniorPolicyList.add(new PolicyInfo("按月（月内按周）循环", 
                    QuickSetting.class.getMethod("monthAndWeekDayCycle", 
                            new Class[]{char[].class, char[].class, char[].class}),
                            new Object[]{new char[]{'1','1','1','1','1','1',
                                                        '1','1','1','1','1','1'},
                                          new char[]{'1','1','1','1','1','1'},
                                          new char[]{'1','1','1','1','1','1','1'}}));
            SeniorPolicyList.add(new PolicyInfo("按月（月内按日）循环", 
                    QuickSetting.class.getMethod("monthAndDayCycle", 
                            new Class[]{char[].class, char[].class, int.class}),
                            new Object[]{
                                new char[]{'1','1','1','1','1','1',
                                            '1','1','1','1','1','1'}, 
                                new char[]{'1','1','1','1','1','1',
                                            '1','1','1','1','1','1',
                                            '1','1','1','1','1','1',
                                            '1','1','1','1','1','1',
                                            '1','1','1','1','1','1',
                                            '1','1'}, 
                                0}));
        } 
        catch (Exception e)
        {
            Util.log_ex(e);
        } 
        mFragmentList.add(new FragmentLoopPolicy(this, SeniorPolicyList));
        mTitleList.add("高级");

        //中国节日
        List<PolicyInfo> ChineseFestivalPolicyList = new ArrayList<PolicyInfo>();
        try
        {
            //1月1日
            ChineseFestivalPolicyList.add(new PolicyInfo("元旦", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{1, 1, 0}));
            //农历正月初一
            ChineseFestivalPolicyList.add(new PolicyInfo("春节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{1, 1, 1}));
            //农历正月十五
            ChineseFestivalPolicyList.add(new PolicyInfo("元宵节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{1, 15, 1}));
            //5月1日
            ChineseFestivalPolicyList.add(new PolicyInfo("劳动节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{5, 1, 0}));
            //农历五月初五
            ChineseFestivalPolicyList.add(new PolicyInfo("端午节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{5, 5, 1}));
            //农历七月初七
            ChineseFestivalPolicyList.add(new PolicyInfo("七夕节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{7, 7, 1}));
            //农历八月十五
            ChineseFestivalPolicyList.add(new PolicyInfo("中秋节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{8, 15, 1}));
            //10月1日
            ChineseFestivalPolicyList.add(new PolicyInfo("国庆节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{10, 1, 0}));
            
        } 
        catch (Exception e)
        {
            Util.log_ex(e);
        } 
        mFragmentList.add(new FragmentLoopPolicy(this, ChineseFestivalPolicyList));
        mTitleList.add("中国节日");
        
        //西方节日
        List<PolicyInfo> WestFestivalPolicyList = new ArrayList<PolicyInfo>();
        try
        {
            //1.1
            WestFestivalPolicyList.add(new PolicyInfo("新年", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{1, 1, 0}));
            //2.14
            WestFestivalPolicyList.add(new PolicyInfo("情人节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{2, 14, 0}));
            //4.1
            WestFestivalPolicyList.add(new PolicyInfo("愚人节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{4, 1, 0}));
            //5月第二个星期日
            WestFestivalPolicyList.add(new PolicyInfo("母亲节", 
                    QuickSetting.class.getMethod("monthAndWeekDayCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{5, 2, Calendar.SUNDAY}));
            //6月第三个星期日
            WestFestivalPolicyList.add(new PolicyInfo("父亲节", 
                    QuickSetting.class.getMethod("monthAndWeekDayCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{6, 3, Calendar.SUNDAY}));
            //10月31日夜
            WestFestivalPolicyList.add(new PolicyInfo("万圣夜", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{10, 31, 0}));
            //11.1
            WestFestivalPolicyList.add(new PolicyInfo("万圣节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{11, 1, 0}));
            //11月的第四个星期四
            WestFestivalPolicyList.add(new PolicyInfo("感恩节", 
                    QuickSetting.class.getMethod("monthAndWeekDayCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{11, 4, Calendar.THURSDAY}));
            //12.24
            WestFestivalPolicyList.add(new PolicyInfo("平安夜", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{12, 24, 0}));
            //12.25
            WestFestivalPolicyList.add(new PolicyInfo("圣诞节", 
                    QuickSetting.class.getMethod("annualCycle", 
                            new Class[]{int.class, int.class, int.class}),
                            new Object[]{12, 25, 0}));
        } 
        catch (Exception e)
        {
            Util.log_ex(e);
        } 
        mFragmentList.add(new FragmentLoopPolicy(this, WestFestivalPolicyList));
        mTitleList.add("西方节日");

        super.setFragmentList(mFragmentList, mTitleList);
    }

    private void updateLayout()
    {
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        StatService.onPause(this);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        StatService.onResume(this);
    }

    public class NewPolicyListener implements OnClickListener 
    {
        Method mMethod;
        Object mArgs[];
        
        NewPolicyListener(Method method, Object args[])
        {
            mMethod = method;
            if(args != null)
            {
                mArgs = args.clone();
            }
        }
        
        @Override
        public void onClick(View arg0)
        {
            if(mMethod == null)
            {
                Toast.makeText(FragmentActivityAddLoopPolicy.this, "未完成", Toast.LENGTH_SHORT).show();
                return;
            }
            
            try
            {
                mMethod.setAccessible(true);
                TLoopPolicy policy = (TLoopPolicy) mMethod.invoke(null, mArgs);
                
                Intent i = new Intent();
                i.putExtra(TimerMgr.ALARM_INTENT_LOOP_POLICY, policy);
                setResult(RESULT_OK, i);
              
                Util.log("Add new policy: " + policy.toString() + ": " + policy.toString());
                finish();
            } catch (Exception e)
            {
                Util.log_ex(e);
            } 
        }
        
    }

    public class FragmentLoopPolicy extends Fragment 
    {

        Context mContext;
        List<PolicyInfo> mPolicyList;
        
        private View mView;
        private ViewGroup mViewGroup;

        public FragmentLoopPolicy(Context context, List<PolicyInfo> policyList)
        {
            super();
            
            mContext = context;
            mPolicyList = policyList;
        }

        @Override
        public void onPause()
        {
            super.onPause();
        }

        public View makeView()
        {
            ScrollView scrollView = new ScrollView(mContext);
            scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            scrollView.setFillViewport(true);
            
            LineBreakLayout layout = new LineBreakLayout(mContext);  
            layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            layout.setPadding(0, 20, 0, 20);
            
            for(int i = 0; i < mPolicyList.size(); i++)
            {
                Button btn = new Button(mContext);  
                btn.setTextAppearance(mContext, R.style.NewLoopPolicyBtnText);
                btn.setBackgroundResource(R.drawable.btn_new_policy_selector);
                btn.setText(mPolicyList.get(i).info);
                btn.setOnClickListener(new NewPolicyListener(mPolicyList.get(i).method, mPolicyList.get(i).args));
                layout.addView(btn);
            }
            scrollView.addView(layout);
            
            return scrollView;
        }
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {                        
            if(mViewGroup != null)
            {
                mViewGroup.removeView(mView);
            }
            mViewGroup = new LinearLayout(mContext);
            
            if(mView == null)
            {
                mView = this.makeView();
            }
            
            mViewGroup.addView(mView);
            
            return mViewGroup;
        }
    }

    public class PolicyInfo
    {
        public String info;
        public Method method;
        public Object args[];
        
        PolicyInfo(String i, Method m, Object a[])
        {
            info = i;
            method = m;
            if(a != null)
            {
                args = a.clone();
            }
        }
        
        PolicyInfo(String i, Method m)
        {
            info = i;
            method = m;
            args = null;
        }

        PolicyInfo()
        {
            
        }
    }

    @Override
    protected void doOnClickSave()
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void doOnClickCancel()
    {
        // TODO Auto-generated method stub
        
    }

}
